﻿using AutoMapper;
using B.S.Filtration.Domain;
using B.S.Filtration.ErrorCode;
using B.S.Filtration.Write.Api.Application.Command.ProcessCommand;
using MediatR;
using SqlSugar;

namespace B.S.Filtration.Write.Api.Application.CommandHander.ProcessHandler
{
    public class AddProcessHandler : IRequestHandler<AddProcessCommand, APIResult<object>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;
        public AddProcessHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }
        public async Task<APIResult<object>> Handle(AddProcessCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var model = mapper.Map<Process>(request);
            //string yeartime = DateTime.Now.ToString("yyyyMMddHHmmss");
            //string ProductCodess1 = "SCLL" + yeartime;
            //model.DocumentNo = ProductCodess1;
            model.IsDel = false;
           

            result.data = await db.Insertable(model).ExecuteCommandAsync();
            if (result.data != null)
            {
                result.code = APIResultCode.success;
                result.msg = "工序列表添加成功";
            }
            else
            {
                result.code = APIResultCode.failed;
                result.msg = "工序列表添加失败";
            }
            return result;
        }
    }
}
